#include <bits/stdc++.h>
#define re register

using namespace std;

const int N = 4e5 + 10;
int n,ans;
int fp[N],sz[N],cnt[N];

inline int read(){
    int r = 0,w = 1;
    char c = getchar();
    while (c < '0' || c > '9'){
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9'){
        r = (r << 3) + (r << 1) + (c ^ 48);
        c = getchar();
    }
    return r * w;
}

inline int find(int x){
    if (x == fp[x]) return fp[x];
    else return fp[x] = find(fp[x]);
}

inline void merge(int a,int b){
    int x = find(a),y = find(b); cnt[x]++;
    if (x != y) sz[x] += sz[y],cnt[x] += cnt[y],fp[y] = x;
}

int main(){
    freopen("pairint.in","r",stdin);
    freopen("pairint.out","w",stdout);
    n = read();
    for (re int i = 1;i <= n * 2;i++) sz[fp[i] = i] = 1;
    for (re int i = 1;i <= n;i++) merge(read(),read());
    for (re int i = 1;i <= n * 2;i++){
        if (i == find(i)){
            if (cnt[i] >= sz[i]) ans += sz[i];
            else ans += (sz[i] - 1);
        }
    } printf("%d",ans);
    return 0;
}